/*
 * Copyright 2010 Mttang.com All right reserved. This software is the confidential and proprietary information of
 * Mttang.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with Mttang.com.
 */
package com.mttang.sys.biz.service.support;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mttang.framework.core.service.AbstractService;
import com.mttang.sys.biz.service.MenuService;
import com.mttang.sys.dto.MenuDTO;
import com.mttang.sys.dto.MenuPrivilegeDTO;
import com.mttang.sys.integration.dao.MenuDAO;
import com.mttang.sys.integration.dao.MenuPrivilegeDAO;
import com.mttang.util.SysBeanConstant;

/**
 * 类MenuServiceImpl.java的实现描述：TODO 类实现描述
 * 
 * @author acc 2010-6-1 上午11:23:50
 */
public class MenuServiceImpl extends AbstractService implements MenuService {

    private Log logger = LogFactory.getLog(getClass());

    /*
     * (non-Javadoc)
     * @see com.mttang.sys.biz.service.MenuService#delete(com.mttang.sys.dto.MenuDTO)
     */
    @Override
    public void delete(MenuDTO dto,Map<Object, Object> logMap) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---MenuServiceImpl---delete");
        }
        MenuDAO dao = (MenuDAO) context.getBean(SysBeanConstant.DAO_MENU);
        MenuPrivilegeDAO menuprv = (MenuPrivilegeDAO) context.getBean(SysBeanConstant.DAO_MENU_PRIVILEGE);
        List<MenuPrivilegeDTO> list = new ArrayList<MenuPrivilegeDTO>();
        MenuPrivilegeDTO mpdto = new MenuPrivilegeDTO();
        mpdto.setLinkId(-1);
        list.add(mpdto);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("linklist", list);
        map.put("menuId", dto.getMenuId());
        menuprv.delete(map);
        dao.delete(dto);
        
        
      //写日志
//        SystemLogService systemLogService = (SystemLogServiceImpl)context.getBean(SysBeanConstant.SERVICE_SYSTEMLOG);
//        systemLogService.addLog(logMap);
    }

    /*
     * (non-Javadoc)
     * @see com.mttang.sys.biz.service.MenuService#findById(java.lang.Integer)
     */
    @Override
    public MenuDTO findById(Integer id) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---MenuServiceImpl---findById");
        }
        MenuDAO dao = (MenuDAO) context.getBean(SysBeanConstant.DAO_MENU);
        return dao.findById(id);
    }

    /*
     * (non-Javadoc)
     * @see com.mttang.sys.biz.service.MenuService#findCount(java.util.Map)
     */
    @Override
    public Long findCount(Map<String, Object> param) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---MenuServiceImpl---findCount");
        }
        MenuDAO dao = (MenuDAO) context.getBean(SysBeanConstant.DAO_MENU);
        return dao.findCount(param);
    }

    /*
     * (non-Javadoc)
     * @see com.mttang.sys.biz.service.MenuService#findUser(java.util.Map)
     */
    @Override
    public List<MenuDTO> findMenu(Map<String, Object> param) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---MenuServiceImpl---findMenu");
        }
        MenuDAO dao = (MenuDAO) context.getBean(SysBeanConstant.DAO_MENU);
        return dao.findMenu(param);
    }

    /*
     * (non-Javadoc)
     * @see com.mttang.sys.biz.service.MenuService#findUser(java.util.Map)
     */
    @Override
    public List<MenuDTO> findMenuTree(Map<String, Object> param) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---MenuServiceImpl---findMenuTree");
        }
        MenuDAO dao = (MenuDAO) context.getBean(SysBeanConstant.DAO_MENU);
        return dao.findMenuTree(param);
    }

    /*
     * (non-Javadoc)
     * @see com.mttang.sys.biz.service.MenuService#insert(com.mttang.sys.dto.MenuDTO)
     */
    @Override
    public void insert(MenuDTO dto,Map<Object, Object> logMap) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---MenuServiceImpl---insert");
        }
        MenuDAO dao = (MenuDAO) context.getBean(SysBeanConstant.DAO_MENU);
        dao.insert(dto);
        MenuPrivilegeDAO menuprv = (MenuPrivilegeDAO) context.getBean(SysBeanConstant.DAO_MENU_PRIVILEGE);
        List<MenuPrivilegeDTO> list = new ArrayList<MenuPrivilegeDTO>();
        if (dto.getPrv() != null && dto.getPrv().length>0) {
            for (int i = 0; i < dto.getPrv().length; i++) {
                MenuPrivilegeDTO mpdto = new MenuPrivilegeDTO();
                mpdto.setLinkId(Integer.valueOf(dto.getPrv()[i].split(",")[0]));
                mpdto.setPrvId(Integer.valueOf(dto.getPrv()[i].split(",")[1]));
                list.add(mpdto);
            }
        }
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                  if(list.get(i).getLinkId()==-1){
                     menuprv.insertMenu(list.get(i)); 
               }
            }
        }
//        //写日志
//        SystemLogService systemLogService = (SystemLogServiceImpl)context.getBean(SysBeanConstant.SERVICE_SYSTEMLOG);
//        systemLogService.addLog(logMap);
    }

    /*
     * (non-Javadoc)
     * @see com.mttang.sys.biz.service.MenuService#update(com.mttang.sys.dto.MenuDTO)
     */
    @Override
    public void update(MenuDTO dto,Map<Object, Object> logMap) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---MenuServiceImpl---update");
        }
        MenuDAO dao = (MenuDAO) context.getBean(SysBeanConstant.DAO_MENU);
        dao.update(dto);
        MenuPrivilegeDAO menuprv = (MenuPrivilegeDAO) context.getBean(SysBeanConstant.DAO_MENU_PRIVILEGE);
        List<MenuPrivilegeDTO> list = new ArrayList<MenuPrivilegeDTO>();
        if (dto.getPrv() != null && dto.getPrv().length>0) {
            for (int i = 0; i < dto.getPrv().length; i++) {
                MenuPrivilegeDTO mpdto = new MenuPrivilegeDTO();
                mpdto.setLinkId(Integer.valueOf(dto.getPrv()[i].split(",")[0]));
                mpdto.setPrvId(Integer.valueOf(dto.getPrv()[i].split(",")[1]));
                mpdto.setMenuId(dto.getMenuId());
                list.add(mpdto);
            }
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("menuId", dto.getMenuId());
        map.put("linklist", list);
        menuprv.delete(map);
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                  if(list.get(i).getLinkId()==-1){
                     menuprv.insert(list.get(i)); 
               }
            }
        }
//        //写日志
//        SystemLogService systemLogService = (SystemLogServiceImpl)context.getBean(SysBeanConstant.SERVICE_SYSTEMLOG);
//        systemLogService.addLog(logMap);
    }

    /* (non-Javadoc)
     * @see com.mttang.sys.biz.service.MenuService#findMenuTop(java.util.Map)
     */
    @Override
    public List<MenuDTO> findMenuTop(Map<String, Object> param) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---MenuServiceImpl---findMenuTop");
        }
        MenuDAO dao = (MenuDAO) context.getBean(SysBeanConstant.DAO_MENU);
        return dao.findMenuTop(param);
    }

    /* (non-Javadoc)
     * @see com.mttang.sys.biz.service.MenuService#findMenuLeft(java.lang.Integer)
     */
    @Override
    public List<MenuDTO> findMenuLeft(Map<String, Object> param) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---MenuServiceImpl---findMenuLeft");
        }
        MenuDAO dao = (MenuDAO) context.getBean(SysBeanConstant.DAO_MENU);
        return dao.findMenuLeft(param);
    }

    /* (non-Javadoc)
     * @see com.mttang.sys.biz.service.MenuService#findByPid(java.lang.Integer)
     */
    @Override
    public Long findByPid(Integer id) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---MenuServiceImpl---findByPid");
        }
        MenuDAO dao = (MenuDAO) context.getBean(SysBeanConstant.DAO_MENU);
        return dao.findByPid(id);
    }

}
